| Conditions | 1 |
| Paths | 3 |
| Total Lines | 116 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /* global API */ |
||
| 36 | .controller('AddAccountCtrl', ['$scope', '$timeout', '$location', '$rootScope', 'StepsService', 'notify', function ($scope, $timeout, $location, $rootScope, StepsService, notify) { |
||
| 37 | $scope.settings = { |
||
| 38 | nextcloud_host: 'https://ncdev.local', |
||
| 39 | nextcloud_username: 'sander', |
||
| 40 | nextcloud_password: 'test', |
||
| 41 | }; |
||
| 42 | |||
| 43 | |||
| 44 | $scope.vaults = []; |
||
| 45 | |||
| 46 | $scope.gogo = function (to) { |
||
| 47 | StepsService.steps().goTo(to); |
||
| 48 | }; |
||
| 49 | |||
| 50 | |||
| 51 | notify.config({ |
||
| 52 | 'position': 'left', |
||
| 53 | 'duration': 2500 |
||
| 54 | }); |
||
| 55 | |||
| 56 | $scope.check = { |
||
| 57 | server: function (callback) { |
||
| 58 | if (!$scope.settings.nextcloud_host || !$scope.settings.nextcloud_username || !$scope.settings.nextcloud_password) { |
||
| 59 | $scope.errors.push(API.i18n.getMessage('invalid_server_settings')); |
||
| 60 | callback(false); |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | $scope.settings.nextcloud_host = $scope.settings.nextcloud_host.replace(/\/$/, ""); |
||
| 64 | PAPI.host = $scope.settings.nextcloud_host; |
||
| 65 | PAPI.username = $scope.settings.nextcloud_username; |
||
| 66 | PAPI.password = $scope.settings.nextcloud_password; |
||
| 67 | PAPI.getVaults(function (vaults) { |
||
| 68 | if (vaults.hasOwnProperty('error')) { |
||
| 69 | var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]); |
||
| 70 | $scope.errors.push(errors); |
||
| 71 | notify(errors); |
||
| 72 | callback(false); |
||
| 73 | } |
||
| 74 | else { |
||
| 75 | $scope.vaults = vaults; |
||
| 76 | callback(true); |
||
| 77 | } |
||
| 78 | $scope.$apply(); |
||
| 79 | }); |
||
| 80 | }, |
||
| 81 | vault: function (callback) { |
||
| 82 | try { |
||
| 83 | PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password); |
||
| 84 | callback(true); |
||
| 85 | } |
||
| 86 | catch (e) { |
||
| 87 | $scope.errors.push(); |
||
| 88 | notify(API.i18n.getMessage('invalid_vault_password')); |
||
| 89 | callback(false); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | }; |
||
| 93 | $scope.saving = false; |
||
| 94 | $scope.next = function () { |
||
| 95 | $scope.saving = true; |
||
| 96 | $scope.errors = []; |
||
| 97 | $timeout(function () { |
||
| 98 | var step = StepsService.getCurrent().name; |
||
| 99 | var check = $scope.check[step]; |
||
| 100 | if (typeof check === "function") { |
||
| 101 | check(function (result) { |
||
| 102 | $scope.saving = false; |
||
| 103 | if (result) { |
||
| 104 | $scope.errors = []; |
||
| 105 | $scope.$apply(); |
||
| 106 | StepsService.steps().next(); |
||
| 107 | } |
||
| 108 | $timeout(function () { |
||
| 109 | $scope.errors = []; |
||
| 110 | $scope.$apply(); |
||
| 111 | }, 5000); |
||
| 112 | }); |
||
| 113 | } |
||
| 114 | else { |
||
| 115 | $scope.saving = false; |
||
| 116 | StepsService.steps().next(); |
||
| 117 | } |
||
| 118 | }, 10); |
||
| 119 | }; |
||
| 120 | |||
| 121 | $scope.cancelAdd = function () { |
||
| 122 | window.location = '#!/settings/2'; |
||
| 123 | }; |
||
| 124 | |||
| 125 | $scope.finished = function () { |
||
| 126 | var _settings = angular.copy($scope.settings); |
||
| 127 | |||
| 128 | var account = { |
||
| 129 | nextcloud_host: _settings.nextcloud_host, |
||
| 130 | nextcloud_username: _settings.nextcloud_username, |
||
| 131 | nextcloud_password: _settings.nextcloud_password, |
||
| 132 | vault: _settings.default_vault, |
||
| 133 | vault_password: _settings.vault_password |
||
| 134 | }; |
||
| 135 | $scope.saving = true; |
||
| 136 | API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) { |
||
| 137 | settings.accounts.push(account); |
||
| 138 | API.runtime.sendMessage(API.runtime.id, { |
||
| 139 | method: "saveSettings", |
||
| 140 | args: settings |
||
| 141 | }).then(function () { |
||
| 142 | setTimeout(function () { |
||
| 143 | notify(API.i18n.getMessage('account_added')); |
||
| 144 | $scope.saving = false; |
||
| 145 | window.location = '#!/settings/2'; |
||
| 146 | }, 750); |
||
| 147 | }); |
||
| 148 | |||
| 149 | }); |
||
| 150 | }; |
||
| 151 | }]); |
||
| 152 | }()); |
||
| 154 |